package com.hymall.service.wx;

import com.hymall.bean.MarketCategory;
import com.hymall.bean.MarketCategoryExample;
import com.hymall.bean.vo.WxCatagoryDetailVO;
import com.hymall.bean.vo.WxCatagoryIndexVO;
import com.hymall.mapper.MarketCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 长平居士 - rolfcui@outlook.com
 * @description 目录模块 服务实现
 * @date 2022/7/19 15:32
 */
@Service
public class CatalogServiceImpl implements CatalogService {

    @Autowired
    MarketCategoryMapper marketCategoryMapper;

    @Override
    public WxCatagoryIndexVO query() {
        WxCatagoryIndexVO vo = new WxCatagoryIndexVO();
        MarketCategoryExample example = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andLevelEqualTo("L1");
        List<MarketCategory> list = marketCategoryMapper.selectByExample(example);
        if (list.isEmpty()) {
            return vo;
        }
        vo.setCurrentCategory(list.get(0));
        vo.setCategoryList(list);
        example.clear();

        criteria = example.createCriteria();
        criteria.andLevelEqualTo("L2");
        criteria.andPidEqualTo(list.get(0).getId());
        vo.setCurrentSubCategory(marketCategoryMapper.selectByExample(example));

        return vo;
    }

    @Override
    public WxCatagoryDetailVO current(Integer id) {
        WxCatagoryDetailVO vo = new WxCatagoryDetailVO();
        vo.setCurrentCategory(marketCategoryMapper.selectByPrimaryKey(id));
        MarketCategoryExample example = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andLevelEqualTo("L2");
        criteria.andPidEqualTo(id);
        vo.setCurrentSubCategory(marketCategoryMapper.selectByExample(example));
        return vo;
    }
}
